package com.jykj.modbus.manage;

import com.jykj.modbus.equipment.entity.CollectInformation;
import com.jykj.util.config.AppConfig;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by mwh
 * 2021-11-2
 */
public class AgreementEle {
    public static  Map<String , Object> analysis(String command) throws Exception {
        Map<String , Object> mapData = new HashMap<>();
        List<CollectInformation> collectData = new ArrayList<CollectInformation>();
        String regex = "(.{2})";
        command = command.replaceAll(regex, "$1 ");
        String[] commands=command.trim().split(" ");
        List<String> list =new ArrayList<String>();
        for (int i = 0; i <commands.length ; i++) {
            if (!commands[i].equalsIgnoreCase("FE")){
                list.add(commands[i]);
            }
        }
        //解析报文格式
        String[] newCommands=list.toArray(new String[list.size()]);

        if (newCommands.length<16||newCommands.length>22||Integer.parseInt(newCommands[0])!=68||Integer.parseInt(newCommands[newCommands.length-1])!=16){
            System.err.print("非法帧，无法解析！");
            return mapData;
        }else {
            System.out.println("您的输入：" + command);
            System.out.println("原始地址：" + list);
            System.out.println("帧起始符：" + newCommands[0]);
            //解析地址码(帧起始符)
            mapData.put("addressID" , newCommands[0]);
            System.out.println("控制域：" + newCommands[8]);
            //解析功能码(控制域)
            mapData.put("functionID" , newCommands[8]);
            System.out.println("电表地址：" + Byte.parseByte(newCommands[6]) + Byte.parseByte(newCommands[5]) + Byte.parseByte(newCommands[4]) + Byte.parseByte(newCommands[3]) + Byte.parseByte(newCommands[2]) + Byte.parseByte(newCommands[1]));
            //电表地址
            mapData.put("eleNum" , Byte.parseByte(newCommands[6]) + Byte.parseByte(newCommands[5]) + Byte.parseByte(newCommands[4]) + Byte.parseByte(newCommands[3]) + Byte.parseByte(newCommands[2]) + Byte.parseByte(newCommands[1]));
            System.out.println("数据域长度：" + newCommands[9]);
            //有效字节数(数据域长度)
            mapData.put("significantByte" , newCommands[9]);
            System.out.println("校验码：" + newCommands[newCommands.length - 2]);
            //解析校验码低位
            mapData.put("checkLow" , newCommands[newCommands.length - 2]);
            System.out.println("停止位：" + newCommands[newCommands.length - 1]);
            //解析校验码高位
            mapData.put("checkHigh" , newCommands[newCommands.length - 1]);
            //int DTID=newCommands[newCommands.length - 2 - newCommands[9]];
            //解析数据标识
            List<String> list2 = new ArrayList<String>();
            for (int i = 0; i < 4; i++) {
                list2.add(Integer.toHexString(Integer.parseInt(newCommands[newCommands.length - 3 - i-(Integer.parseInt(newCommands[9],16)-4)], 16) - 51));
            }
            String[] DTID = list2.toArray(new String[list2.size()]);
            StringBuffer sbr = new StringBuffer();
            for (int i = 0; i < DTID.length; i++) {
                if (DTID[i].length() == 1) {
                    DTID[i] = String.format("%02d", Integer.parseInt(DTID[i]));
                }else if (DTID[i].length() == 8){
                    DTID[i] = "FF";
                }
                sbr.append(DTID[i]);
            }
            //加载文件，取值
            String uploadAddress = AppConfig.getProperty("uploadAddress");
            InputStream is = new BufferedInputStream(new FileInputStream("E:/wulian/jicunqi/config.properties"));
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            Properties properties = new Properties();
            try {
                properties.load(isr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(sbr.toString());
            System.out.println("数据项名称：" + properties.getProperty(sbr.toString()));
            //数据项名称
            mapData.put("dataName",properties.getProperty(sbr.toString()));
            //解析返回数据
            //68 61 01 00 00 00 00 68 91 06 33 33 39 35 34 38 09 16
            //68 21 45 00 20 62 21 68 91 08 33 33 34 33 3C 86 36 33 6A 16
            if (newCommands.length>16){
                int DTID0=Integer.parseInt(DTID[0]);
                int DTID1=Integer.parseInt(DTID[1]);
                List<String> list3=new ArrayList();
                for (int i = 0; i < Integer.parseInt(newCommands[9],16)-4; i++) {
                    list3.add(newCommands[newCommands.length - 3 - i]);
                }
                String[] data = list3.toArray(new String[list3.size()]);
                long num = Long.parseLong((DataFormat(data)).toString());
                BigDecimal bigDecimal = new BigDecimal(num);
                if (DTID0==2&&DTID1==1&&!String.valueOf(DTID[2]).equals("FF")){
                    //电压0.1v
                    CollectInformation collect1 = new CollectInformation();
                    mapData.put("value" , properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.1")));
                    collect1.setTypeName(properties.getProperty(sbr.toString()));
                    collect1.setValue(bigDecimal.multiply(new BigDecimal("0.1")).toString());
                    collect1.setUnit("V");
                    if (properties.getProperty(sbr.toString()).equals("A相电压")){
                        collect1.setType("211");
                    }else if (properties.getProperty(sbr.toString()).equals("B相电压")){
                        collect1.setType("212");
                    }else if (properties.getProperty(sbr.toString()).equals("C相电压")){
                        collect1.setType("213");
                    }
                    collectData.add(collect1);
                    mapData.put("CollectInformation" , collectData);
                    System.out.println(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.1"))+"v");
                }else if (DTID0==2&&DTID1==2){
                    //电流0.001A
                    CollectInformation collect1 = new CollectInformation();
                    mapData.put("value" , properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001")));
                    collect1.setTypeName(properties.getProperty(sbr.toString()));
                    collect1.setValue(bigDecimal.multiply(new BigDecimal("0.001")).toString());
                    collect1.setUnit("A");
                    if (properties.getProperty(sbr.toString()).equals("A相电流")){
                        collect1.setType("221");
                    }else if (properties.getProperty(sbr.toString()).equals("B相电流")){
                        collect1.setType("222");
                    }else if (properties.getProperty(sbr.toString()).equals("C相电流")){
                        collect1.setType("223");
                    }
                    collectData.add(collect1);
                    mapData.put("CollectInformation" , collectData);
                    System.out.println(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001"))+"A");
                }else if ((DTID0==2&&DTID1==3)||(DTID0==2&&DTID1==4)||(DTID0==2&&DTID1==5)){
                    //有无功功率0.0001
                    CollectInformation collect1 = new CollectInformation();
                    mapData.put("value" , properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001")));
                    collect1.setTypeName(properties.getProperty(sbr.toString()));
                    collect1.setValue(bigDecimal.multiply(new BigDecimal("0.001")).toString());
                    collect1.setUnit("Kwh");
                    collect1.setType("202");
                    collectData.add(collect1);
                    mapData.put("CollectInformation" , collectData);
                    System.out.println(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.0001")));

                }else if (DTID0==2&&DTID1==6){
                    //功率因数0.001
                    CollectInformation collect1 = new CollectInformation();
                    mapData.put("value" , properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001")));
                    collect1.setTypeName(properties.getProperty(sbr.toString()));
                    collect1.setValue(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001")));
                    collect1.setUnit("功率因数");
                    collectData.add(collect1);
                    mapData.put("CollectInformation" , collectData);
                    System.out.println(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.001")));

                }else if ((DTID0==0&&DTID1==0)||(DTID0==0&&DTID1==1)||(DTID0==0&&DTID1==2)||(DTID0==0&&DTID1==3)||(DTID0==0&&DTID1==4)||(DTID0==0&&DTID1==5)||(DTID0==0&&DTID1==6)||(DTID0==0&&DTID1==7)||(DTID0==0&&DTID1==8)){
                    //有无功总电能、四象限无功总电能0.01
                    CollectInformation collect1 = new CollectInformation();
                    mapData.put("value" , bigDecimal.multiply(new BigDecimal("0.01")));
                    collect1.setTypeName(properties.getProperty(sbr.toString()));
                    collect1.setValue(bigDecimal.multiply(new BigDecimal("0.01")).toString());
                    collect1.setUnit("Kwh");
                    collect1.setType("202");
                    collectData.add(collect1);
                    mapData.put("CollectInformation" , collectData);
                    System.out.println(properties.getProperty(sbr.toString())+"："+bigDecimal.multiply(new BigDecimal("0.01")));

                }else if (DTID0==2&&DTID1==1&&String.valueOf(DTID[2]).equals("FF")){
                    //电压数据块
                    System.out.println(String.valueOf(num));
                    System.out.println(String.valueOf(num).substring(0,4));
                    System.out.println(String.valueOf(num).substring(4,8));
                    System.out.println(String.valueOf(num).substring(8));

                    CollectInformation collect1 = new CollectInformation();
                    System.out.println("C相电压"+new BigDecimal(String.valueOf(num).substring(0,4)).multiply(new BigDecimal("0.1")));
                    System.out.println("B相电压"+new BigDecimal(String.valueOf(num).substring(4,8)).multiply(new BigDecimal("0.1")));
                    System.out.println("A相电压"+new BigDecimal(String.valueOf(num).substring(8)).multiply(new BigDecimal("0.1")));

                }else{
                    System.out.println(properties.getProperty(sbr.toString())+"："+num);
                }
            }
            return mapData;
        }
    }
    public static StringBuffer DataFormat(String data[]){
        StringBuffer sbr=new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            String data1=String.valueOf(Integer.parseInt(data[i].substring(0,1),16)-3);
            String data2=String.valueOf(Integer.parseInt(data[i].substring(1),16)-3);
            sbr.append(data1);
            sbr.append(data2);
        }
        return sbr;
    }

//    public static void main(String[] args) {
//        byte[] msg=new byte[3];
//        msg[1]=26;
//        msg[2]=3;
//        int j=Integer.parseInt((new StringBuffer(msg[2]).append(msg[1])).toString());
//        System.out.println(j);
//        String i=Integer.toBinaryString(j);
//        System.out.println(i);
//        /*System.out.println("请输入命令");
//        Scanner sc=new Scanner(System.in);
//        String command=sc.nextLine().replace(",","");
//        System.out.println(command);*/
//      /*  List list=new ArrayList();
//        for (int i = 0; i < 10; i++) {
//            list.add("10 ");
//        }
//        System.out.println(list);
//        System.out.println(list.toString().replace("\\s*","").replaceAll("\\[|\\]",""));*/
//        AgreementEle ad=new AgreementEle();
////        "fe fe 68 61 01 00 00 00 00 68 91 06 33 33 39 35 34 38 09 16 "
//        //"FE FE 68 21 45 00 20 62 21 68 91 08 33 33 34 33 3C 86 36 33 6A 16 "
//        //"FE FE 68 98 44 00 20 62 21 68 91 07 33 34 35 35 B3 47 33 E5 16"
//        try {
//            ad.analysis("FEFE6898440020622168910733343535B34733E516");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("解析出错！");
//        }
//    }

}
